<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Home\Widget;
use Think\Controller;

class PublicWidget extends Controller {
	
	/* 根据randcode标识串获取对应的数据信息 */
 	function get_randcode_info($randcode = null){
		$r     = null;
		$rand  = D(C('TABLE_RANDNUMBER'));
		$res   = $rand->field('id,ma_rootid,ma_domainid,ma_randnumber,ma_hb_cishu,ma_type,iswhoget,isvalid,batchnumber')->where(array('ma_randnumber'=>$randcode))->find();
		if($res) $r = $res;
		return $r;
	}
	
	/* 根据ma_type的值查询是什么奖品 */
	function cx_award($ma_type = 1){
		$award = D(C('TABLE_AWARD'));
		if($ma_type != 1){
			$result = $award->find($ma_type);
		}else{
			$result = array('awardkind' => $ma_type);
		}
		return $result;
	}
	
	/* 如果奖品类型为4（流量）则查询流量规则 */
	function cx_flow_rule($award_id, $awardkind){
		if($awardkind != 4 || empty($award_id)){
			$result = null;
		}else{
			$awardflow 	= D(C('TABLE_AWARDFLOW'));
			$result 	= $awardflow->where(array('award_id' => $award_id))->select();
		}
		return $result;
	}
	
	/* 检测手机号码是否能充值 */
	function telcheck($phoneno, $cardnum){
		if(empty($phoneno) || empty($cardnum)) $result = false;
		$api_url 	= C('T_TELCHECK');
		$par = array(
				'phoneno'  	=> $phoneno,
				'cardnum'  	=> $cardnum,
				'key'  		=> C('T_APPKEY')
		);
		$result = http($api_url,$par,'POST');
		return $result;
	}
	
	/* 手机直充业务处理 */
	function onlineorder($phoneno, $cardnum, $randnumber){
		if(empty($phoneno) || empty($cardnum)){
			$result = false;
		}else{
			$api_url 	= C('T_ONLINEORDER');
			$t_openid 	= C('T_OPENID');
			$t_appkey 	= C('T_APPKEY');
			$onlieorder = C('ONLINEORDER');
			//商家订单号，自己生成
			$orderid 	= md5(uniqid());
			$par = array(
					'phoneno'  	=> $phoneno,
					'cardnum'  	=> $cardnum,
					'orderid'  	=> $orderid,
					'key'  		=> $t_appkey,
					'sign'  	=> md5($t_openid.$t_appkey.$phoneno.$cardnum.$orderid)
			);
			$rechargeable_result = http($api_url,$par,'POST');
			//充值成功则保存充值记录
			if($rechargeable_result['error_code'] == 0){
				$result = $this -> save_rechargeable($rechargeable_result['result'], $randnumber, $cardnum);
			}else{
				$result = false;
			}
		}
		return $result;
	}
	/* 保存话费充值记录 */
	function save_rechargeable($rechargeable_result, $randnumber, $cardnum){
		$result 	  = false;
		$mobilecharge = D(C('TABLE_MOBILECHARGELOG'));
		// 启动事务
		$mobilecharge -> startTrans();
		// 增加字段值属性
		$mobilecharge -> ma_id 			= $randnumber['ma_rootid'];
		$mobilecharge -> randnumber 	= $randnumber['ma_randnumber'];
		$mobilecharge -> mobilephone 	= $rechargeable_result['game_userid'];
		$mobilecharge -> money 			= $cardnum;
		$mobilecharge -> ordercash 		= $rechargeable_result['ordercash'];
		$mobilecharge -> cardname 		= $rechargeable_result['cardname'];
		$mobilecharge -> sporder_id 	= $rechargeable_result['sporder_id'];
		$mobilecharge -> uorderid 		= $rechargeable_result['uorderid'];
		$mobilecharge -> useropenid 	= $_COOKIE['user_userinfo'];
		$mobilecharge -> game_state 	= $rechargeable_result['game_state'];;
		$mobilecharge -> cdate 			= date("Y-m-d H:i:m");
		$mobilecharge -> batchnumber	= $randnumber['batchnumber'];
		// 把数据对象添加到数据库
		$res = $mobilecharge -> add();
		if($res){
			if($this -> change_award_status($res, $randnumber)){
				$mobilecharge -> commit();
				$result 	  = true;
			}else{
				$mobilecharge -> rollback();
				$result 	  = false;
			}
		}
		return $result;
	}
	/* 改变此码的奖品领取状态 */
	function change_award_status($rechargeable_id, $randnumber = null){
		if(empty($randnumber)){
			$result = false;
		}else{
			$rand   = D(C('TABLE_RANDNUMBER'));
			$data['ma_hb_cishu'] 	= 1;
			$data['iswhoget'] 		= $rechargeable_id;
			$data['zjtime'] 		= date("Y-m-d H:i:m");
			$result = $rand->where(array('ma_randnumber' => $randnumber['ma_randnumber']))->save($data);
		}
		return $result;
	}
	
	/* 检测号码支持的流量套餐 */
	function telcheck_flow($phoneno = null){
		if(empty($phoneno)) $result = false;
		$api_url 	= C('T_FLOW_TELCHECK');
		$par = array(
				'phone'  	=> $phoneno,
				'key'  		=> C('T_FLOW_APPKEY')
		);
		$result = http($api_url,$par,'POST');
		return $result;
	}
	/* 提交流量充值 */
	function execute_recharge($phoneno, $pid, $randnumber, $setmeal){
		if(empty($phoneno) || empty($pid)){
			$result = array('error_code' => 400, 'reason' => '参数有误');
		}else{
			$api_url 	= C('T_FLOW_RECHARGE');
			$t_openid 	= C('T_OPENID');
			$t_appkey 	= C('T_FLOW_APPKEY');
			//商家订单号，自己生成
			$orderid 	= md5(uniqid());
			$par = array(
					'phone'  	=> $phoneno,
					'pid'  		=> $pid,
					'orderid'  	=> $orderid,
					'key'  		=> $t_appkey,
					'sign'  	=> md5($t_openid.$t_appkey.$phoneno.$pid.$orderid)
			);
			$rechargeable_result = http($api_url,$par,'POST');
			//流量套餐充值成功则保存充值记录
			if($rechargeable_result['error_code'] == 0){
				$res    	= $this -> save_flow_record($rechargeable_result['result'], $pid, $randnumber, $setmeal);
				if($res){
					$result = array('error_code' => 0, 'reason' => '充值成功');
				}else{
					$result = array('error_code' => 11011, 'reason' => '充值失败');
				}
			}else{
				$result = array('error_code' => 11011, 'reason' => '充值失败', 'message' => $result['reason']);
			}
		}
		return $result;
	}
	/* 流量充值充值记录 */
	function save_flow_record($rechargeable_result, $pid, $randnumber, $setmeal){
		$result 	= false;
		$mobileflow = D(C('TABLE_MOBILEFLOWLOG'));
		// 启动事务
		$mobileflow -> startTrans();
		// 增加字段值属性
		foreach ($setmeal['result'][0]['flows'] as $v){
			if($v['id'] == $pid){
				$rechargenum = $v['p'];
			}
		}
		$mobileflow -> ma_id 		= $randnumber['ma_rootid'];
		$mobileflow -> randnumber 	= $randnumber['ma_randnumber'];
		$mobileflow -> mobilephone 	= $rechargeable_result['phone'];
		$mobileflow -> rechargenum 	= $rechargenum;
		$mobileflow -> ordercash 	= $rechargeable_result['ordercash'];
		$mobileflow -> flow_id 		= $pid;
		$mobileflow -> cardname 	= $rechargeable_result['cardname'];
		$mobileflow -> sporder_id 	= $rechargeable_result['sporder_id'];
		$mobileflow -> uorderid 	= $rechargeable_result['orderid'];
		$mobileflow -> company 		= $setmeal['result'][0]['companytype'];
		$mobileflow -> useropenid 	= $_COOKIE['user_userinfo'];
		$mobileflow -> game_state 	= 0;
		$mobileflow -> cdate 		= date("Y-m-d H:i:m");
		$mobileflow -> batchnumber	= $randnumber['batchnumber'];
		// 把数据对象添加到数据库
		$res = $mobileflow -> add();
		if($res){
			if($this -> change_award_status($res, $randnumber)){
				$mobileflow -> commit();
				$result 	= true;
			}else{
				$mobileflow -> rollback();
				$result 	= false;
			}
		}
		return $result;
	}	


}
?>